package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    public ResponseResult findByNameAndPage(ChannelDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        IPage result = page(page, queryWrapper);
        //封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        pageResponseResult.setData(result.getRecords());
        return pageResponseResult;
    }


    public ResponseResult insert(AdChannel adChannel) {
        //判断请求参数是否为空
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //新增时间
        adChannel.setCreatedTime(new Date());
        //判断是否是默认频道
        if (adChannel.getIsDefault()) {
            //判断表中是否有默认频道，有则修改为非默认
            updateDefaultChannel();
        }
        save(adChannel);
        return ResponseResult.okResult();
    }



    public ResponseResult update(AdChannel adChannel) {
        //判断是否为空
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有数据
        AdChannel byId = getById(adChannel.getId());
        if (byId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否是默认频道
        if(adChannel.getIsDefault()!=null){

            if (adChannel.getIsDefault()) {
                //判断表中是否有默认频道，有则修改为非默认
                updateDefaultChannel();
            }
        }
        //修改频道
        updateById(adChannel);
        return ResponseResult.okResult();
    }


    public ResponseResult deleteById(Integer id) {
        //判断是否为空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel byId = getById(id);
        //判断数据是否存在
        if (byId == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否是启动状态

        if (byId.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.START_ERROR);
        }


        //删除根据id
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //判断表中是否有默认频道，有则修改为非默认的方法
    public void updateDefaultChannel() {
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdChannel::getIsDefault, 1);
        AdChannel one = getOne(lambdaQueryWrapper);
        if (one != null) {
            //将数据修改为非默认
            one.setIsDefault(false);
            updateById(one);
        }
    }
}
